package com.ruiysoft.crm.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ruiysoft.crm.dto.CourseEx;
import com.ruiysoft.crm.dto.ExpressInfo;
import com.ruiysoft.crm.dto.OrderDetailEx;
import com.ruiysoft.crm.dto.OrderEx;
import com.ruiysoft.crm.entity.Course;
import com.ruiysoft.crm.entity.Order;
import com.ruiysoft.crm.entity.OrderDetail;
import com.ruiysoft.crm.entity.OrderProgress;
import com.ruiysoft.crm.mapper.OrderDetailMapper;
import com.ruiysoft.crm.mapper.OrderMapper;
import com.ruiysoft.crm.mapper.OrderProgressMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Yanghu
 * @since 2018-09-08
 */
@RestController
@RequestMapping("order")
public class OrderController {
    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderDetailMapper orderDetailMapper;

    @Autowired
    OrderProgressMapper orderProgressMapper;

    @Cacheable("queryByUserId")
    @GetMapping("queryByUserId")
    public Object queryByUserId(String id) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("success", false);

        try {
//            EntityWrapper<Course> entityWrapper = new EntityWrapper();
//            List<Course> list = courseMapper.selectPage(new Page<Course>(1, 25), entityWrapper);
            List<OrderEx> list = orderMapper.selectLeftJoinValueList(id);
            if(list.size() > 0) {
                map.put("success", true);
                map.put("data", list);
            }
            else
                map.put("errorMsg", "查询数据失败");

        } catch (Exception error) {
            map.put("errorMsg", "异常: " + error.getMessage());
        }

        return map;
    }

    @PostMapping("updateExpressInfo")
    public Object updateExpressInfo(@RequestBody ExpressInfo obj) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("success", false);

        try {
            Order order = orderMapper.selectById(obj.getOrderId());
            if(order != null) {
                order.setUpdateTime(new Date());
                order.setStatus("courseStatus_05");
                order.setExpress(obj.getExpressCompany());
                order.setExpressNu(obj.getExpressNu());


                if (orderMapper.updateById(order) > 0) {
                    OrderProgress orderProgress = new OrderProgress();
                    orderProgress.setOrderId(order.getId());
                    orderProgress.setType("3");
                    orderProgress.setCreateTime(new Date());

                    if(orderProgressMapper.insert(orderProgress) > 0)
                        map.put("success", true);
                }
                else
                    map.put("errorMsg", "更新失败!");
            }

        } catch (Exception error) {
            map.put("errorMsg", "create 异常: " + error.getMessage());
        }

        return map;
    }

    @Cacheable("queryDetailByOrderId")
    @GetMapping("queryDetailByOrderId")
    public Object queryDetailByOrderId(String id) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("success", false);

        try {
            List<OrderDetailEx> list = orderDetailMapper.selectLeftJoinCourseByOrderId(id);
            if(list.size() > 0) {
                map.put("success", true);
                map.put("data", list);
            }
            else
                map.put("errorMsg", "查询数据失败");

        } catch (Exception error) {
            map.put("errorMsg", "异常: " + error.getMessage());
        }

        return map;
    }

    String getUUID() {
        UUID uuid = UUID.randomUUID();
        return uuid.toString().replace("-", "");
    }

    @Cacheable("query")
    @GetMapping("query")
    public Object query() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("success", false);

        try {
//            EntityWrapper<Course> entityWrapper = new EntityWrapper();
//            List<Course> list = courseMapper.selectPage(new Page<Course>(1, 25), entityWrapper);
            List<OrderEx> list = orderMapper.selectLeftJoinEmployeeValueList();
            if(list.size() > 0) {
                map.put("success", true);
                map.put("data", list);
            }
            else
                map.put("errorMsg", "查询数据失败");

        } catch (Exception error) {
            map.put("errorMsg", "delete 异常: " + error.getMessage());
        }

        return map;
    }

    @PostMapping("create")
    public Object create(@RequestBody OrderEx orderEx) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("success", false);

//        OrderDetail test = new OrderDetail();
//        test.setSumPrice(1.0f);
//        test.setAmount(100);
//        test.setCreateTime(new Date());
//
//        orderDetailMapper.insert(test);


        try {
            orderEx.setId(getUUID());
            orderEx.setCreateTime(new Date());
            orderEx.setStatus("courseStatus_01");

            if (orderMapper.insert(orderEx) > 0) {
                for (OrderDetail orderDetail: orderEx.getOrderDetail()
                     ) {
                    orderDetail.setOrderId(orderEx.getId());
                    orderDetail.setCreateTime(new Date());

                    if(orderDetailMapper.insert(orderDetail) > 0) {
                        OrderProgress orderProgress = new OrderProgress();
                        orderProgress.setOrderId(orderEx.getId());
                        orderProgress.setType("1");
                        orderProgress.setCreateTime(new Date());

                        if(orderProgressMapper.insert(orderProgress) > 0)
                            map.put("success", true);

                    }
                }

                map.put("data", orderEx);
            }

        } catch (Exception error) {
            map.put("errorMsg", "create 异常: " + error.getMessage());
        }

        return map;
    }
}
